package cmd

import (
	"log"
	"os"

	"gitee.com/lcc_dr/webhook.git/app/dingding"
	"gitee.com/lcc_dr/webhook.git/conf"
	"github.com/spf13/cobra"
)

var (
	version bool
	help    bool
	addr    string
	//	url         string
	//	atUserIdsrl string
	//	atMobiles   string
	DefaultPath string

	isAtAll bool
	rootCmd = &cobra.Command{
		Use:   "webhook",
		Short: "webhook 发送告警",
		Long:  "webhook 发送告警",
		RunE: func(cmd *cobra.Command, args []string) error {
			dingding.DingDingWebhook(addr)

			return nil

		},
		PreRunE: func(cmd *cobra.Command, args []string) error {

			//fmt.Println(DefaultPath)
			//fmt.Println(os.Getwd())

			if len(DefaultPath) == 0 {
				home, err := os.Getwd()
				if err != nil {
					log.Fatal(err)
				}
				DefaultPath = home + "/etc/ding_text.toml"

			}

			err := conf.LoadConfigFromToml(DefaultPath)
			if err != nil {
				log.Fatal(err)
			}
			//fmt.Println(conf.GetConf().At.AtMobiles[0])
			//fmt.Println(conf.GetConf().URL)
			//conf.SetConf(url, atUserIdsrl, atMobiles)

			return nil

		},
	}
)

// Execute executes the root command.
func Execute() error {
	return rootCmd.Execute()
}

func init() {

	f := rootCmd.PersistentFlags()
	//f.BoolVarP(&help, "help", "h", false, "打印帮助信息")

	//f.BoolVarP(&version, "version", "v", false, "webhook 版本信息")
	//f.StringVarP(&url, "url", "u", "", "钉钉url")
	//f.StringVarP(&atUserIdsrl, "atUserIds", "U", "", "钉钉发送人")
	f.StringVarP(&addr, "port", "p", "0.0.0.0:5000", "webhook默认端口")
	f.StringVarP(&DefaultPath, "config", "f", "", "钉钉发送配置 默认路径为: ./etc/ding_text.toml")

	//f.BoolVarP(&isAtAll, "isAtAll", "A", false, "@全体人，默认为false")

	rootCmd.AddCommand(versionCmd)
	//rootCmd.AddCommand(PortCmd)

}
